﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class CurrencyForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string currencyId)
        {
            CurrencyEntity currencyE = new CurrencyEntity();
            CurrencyService currencyS = CurrencyService.GetInstance();
            currencyE.CurrencyId = int.Parse(currencyId);
            currencyE = currencyS.LoadByPrimaryKey(currencyE);
            if (null == currencyE)
                return;
            hdfCurrencyId.Value = currencyE.CurrencyId;
            txtCurrencyName.Text = currencyE.CurrencyName;
            txtCurrencyCode.Text = currencyE.CurrencyCode;
            txtRate.Text = currencyE.Rate.ToString();
            txtCustomFormatting.Text = currencyE.CustomFormatting;
            txtDescription.Text = currencyE.Description;
            ckbIsDefault.Checked = currencyE.IsDefault;
            ckbIsActive.Checked = currencyE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            CurrencyEntity currencyE = new CurrencyEntity();
            CurrencyService currencyS = CurrencyService.GetInstance();
            if ("" != hdfCurrencyId.Value.ToString())
            {
                string currencyId = hdfCurrencyId.Value.ToString();
                currencyE.CurrencyId = int.Parse(currencyId);
                currencyE = currencyS.LoadByPrimaryKey(currencyE);
                currencyE.CurrencyCode = txtCurrencyCode.Text;
                currencyE.CurrencyName = txtCurrencyName.Text;
                currencyE.Rate = decimal.Parse(txtRate.Text);
                currencyE.CustomFormatting = txtCustomFormatting.Text;
                currencyE.Description = txtDescription.Text;
                currencyE.IsDefault = ckbIsDefault.Checked;
                currencyE.IsActive = ckbIsActive.Checked;
                currencyE.UpdatedDate = DateTime.Now;
                currencyE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                currencyS.Update(currencyE);
            }
            else
            {
                currencyE.CurrencyCode = txtCurrencyCode.Text;
                currencyE.CurrencyName = txtCurrencyName.Text;
                currencyE.Rate = decimal.Parse(txtRate.Text);
                currencyE.CustomFormatting = txtCustomFormatting.Text;
                currencyE.Description = txtDescription.Text;
                currencyE.IsDefault = ckbIsDefault.Checked;
                currencyE.IsActive = ckbIsActive.Checked;
                currencyE.CreatedDate = DateTime.Now;
                currencyE.CreatedBy = FSSContext.Current.User.EmployeeId; ;
                currencyE.UpdatedDate = DateTime.Now;
                currencyE.UpdatedBy = FSSContext.Current.User.EmployeeId; ;
                currencyS.Insert(currencyE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}